package com.group1.ws;

import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder.In;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Session;

import com.group1.model.Lecturer;
import com.group1.model.Question;
import com.group1.model.Subject;
import com.group1.model.Topic;
import com.group1.util.SessionFactoryUtil;

@Path("/question")
public class QuestionWS {

	@GET
	@Path("/getAll")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAllQuestion()
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Question> list =  Question.getAllQuestion(session);
		String result;
		result = "{QUESTION:[";
		boolean first = true;
		for (Question q : list) {
			String questType = q.getQuestionType();
			if(questType.equals("1"))
			{
				questType = "Multiple Choice";
			}else{
				if(questType.equals("2"))
				{
					questType = "Fillable Blank";
				}
				else
				{
					if(questType.equals("3"))
					{
						questType = "Matching Items";
					}
					else
					{
						if(questType.equals("4"))
						{
							questType = "Full Text";
						}
						else
						{
							questType = "";
						}
					}
				}
			}
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"questID\"" + ":\"" + q.getQuestionId()+ "\",";
			result += "\"lecturerID\"" + ":\"" + q.getLecturer().getLecturerName() + "\",";
			result += "\"questDesc\"" + ":\"" + q.getQuestionDesc()+ "\",";
			result += "\"questType\"" + ":\"" + questType + "\",";
			result += "\"level\"" + ":\"" + q.getQuestionLevel()+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByID/{questID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getQuestionByID(@PathParam("questID") String questID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Question q = Question.getQuestionByID(Integer.valueOf(questID), session);
		String result;
		String questType = q.getQuestionType();
		if(questType.equals("1"))
		{
			questType = "Multiple Choice";
		}else{
			if(questType.equals("2"))
			{
				questType = "Fillable Blank";
			}
			else
			{
				if(questType.equals("3"))
				{
					questType = "Matching Items";
				}
				else
				{
					if(questType.equals("4"))
					{
						questType = "Full Text";
					}
					else
					{
						questType = "";
					}
				}
			}
		}
		result = "{QUESTION:";
		result += "{";
		result += "\"questID\"" + ":\"" + q.getQuestionId()+ "\",";
		result += "\"lecturerID\"" + ":\"" + q.getLecturer().getLecturerName() + "\",";
		result += "\"questDesc\"" + ":\"" + q.getQuestionDesc()+ "\",";
		result += "\"questType\"" + ":\"" + questType + "\",";
		result += "\"level\"" + ":\"" + q.getQuestionLevel()+ "\"";
		result += "}}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByTopic/{topicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getQuestionByTopic(@PathParam("topicID") String topicID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Collection<Question> list =  Question.getQuestionByTopic(Integer.valueOf(topicID), session);
		String result;
		result = "{QUESTION:[";
		boolean first = true;
		for (Question q : list) {
			String questType = q.getQuestionType();
			if(questType.equals("1"))
			{
				questType = "Multiple Choice";
			}else{
				if(questType.equals("2"))
				{
					questType = "Fillable Blank";
				}
				else
				{
					if(questType.equals("3"))
					{
						questType = "Matching Items";
					}
					else
					{
						if(questType.equals("4"))
						{
							questType = "Full Text";
						}
						else
						{
							questType = "";
						}
					}
				}
			}
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"questID\"" + ":\"" + q.getQuestionId()+ "\",";
			result += "\"lecturerID\"" + ":\"" + q.getLecturer().getLecturerName() + "\",";
			result += "\"questDesc\"" + ":\"" + q.getQuestionDesc()+ "\",";
			result += "\"questType\"" + ":\"" + questType + "\",";
			result += "\"level\"" + ":\"" + q.getQuestionLevel()+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	
	@GET
	@Path("/getByLecturer/{lecturerID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getQuestionByLecturer(@PathParam("lecturerID") String lecturerID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Question> list =  Question.getQuestionByLecturer(lecturerID, session);
		String result;
		result = "{QUESTION:[";
		boolean first = true;
		for (Question q : list) {
			String questType = q.getQuestionType();
			if(questType.equals("1"))
			{
				questType = "Multiple Choice";
			}else{
				if(questType.equals("2"))
				{
					questType = "Fillable Blank";
				}
				else
				{
					if(questType.equals("3"))
					{
						questType = "Matching Items";
					}
					else
					{
						if(questType.equals("4"))
						{
							questType = "Full Text";
						}
						else
						{
							questType = "";
						}
					}
				}
			}
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"questID\"" + ":\"" + q.getQuestionId()+ "\",";
			result += "\"lecturerID\"" + ":\"" + q.getLecturer().getLecturerName() + "\",";
			result += "\"questDesc\"" + ":\"" + q.getQuestionDesc()+ "\",";
			result += "\"questType\"" + ":\"" + questType + "\",";
			result += "\"level\"" + ":\"" + q.getQuestionLevel()+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	
	@GET
	@Path("/create/{lecturerID}/{questDesc}/{questType}/{level}/{topicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String createQuestion(@PathParam("lecturerID") String lecturerID, @PathParam("questDesc") String questDesc, 
			@PathParam("questType") String questType, @PathParam("level") String level,
			@PathParam("topicID") String topicID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			   	Question q = new Question();
			   	q.setLecturer(Lecturer.getLecturerByID(session, lecturerID));
			   	q.setQuestionDesc(questDesc);
			   	q.setQuestionType(questType);
			   	q.setQuestionLevel(Integer.valueOf(level));
			   	q.getTopicQuestionMapping().add(Topic.getTopicByID(Integer.valueOf(topicID), session));
			   	session.save(q);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/update/{questID}/{lecturerID}/{questDesc}/{questType}/{level}")
	@Produces(MediaType.TEXT_PLAIN)
	public String updateQuestion(@PathParam("questID") String questID, @PathParam("lecturerID") String lecturerID, 
			@PathParam("questDesc") String questDesc, @PathParam("questType") String questType,  
			@PathParam("level") String level)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	Question q = Question.getQuestionByID(Integer.valueOf(questID), session);
			 	q.setLecturer(Lecturer.getLecturerByID(session, lecturerID));
			 	q.setQuestionDesc(questDesc);
			 	q.setQuestionType(questType);
			 	q.setQuestionLevel(Integer.valueOf(level));
				session.saveOrUpdate(q);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/delete/{questID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String deleteQuestion(@PathParam("questID") String questID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 session.delete(Question.getQuestionByID(Integer.valueOf(questID), session));
			 session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	
}
